import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//牛客 跳石板
public class Solution14 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int m = in.nextInt();
            int[] dp = new int[m + 1];
            for (int i = 0; i < dp.length; i++) {
                dp[i] = Integer.MAX_VALUE;
            }
            dp[n] = 0;//初始化好dp数组之后才往dp数组的n下标赋值
            for (int i = n; i < m; i++) {
                if(dp[i] == Integer.MAX_VALUE) {
                    continue;
                }
                List<Integer> list = f(i);
                for (int j : list) {
                    if(i + j <= m && dp[i + j] != Integer.MAX_VALUE) {
                        dp[i + j] = Math.min(dp[i + j], dp[i] + 1);
                    } else if (i + j <= m && dp[i + j] == Integer.MAX_VALUE) {
                        dp[i + j] = dp[i] + 1;
                    }
                }
            }
            if(dp[m] == Integer.MAX_VALUE) {
                System.out.println(-1);
            }else {
                System.out.println(dp[m]);
            }
        }
    }
    public static List<Integer> f(int n) {
        List<Integer> list = new ArrayList<>();
        for (int i = 2; i * i <= n; i++) {
            if(n % i == 0) {
                list.add(i);
                if(n / i != i) {
                    list.add(n / i);
                }
            }
        }
        return list;
    }
}
